package com.lingxu.base.system.manage.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lingxu.base.system.manage.entity.MenuTree;
import com.lingxu.base.system.manage.entity.SysMenu;
import com.lingxu.base.system.manage.entity.SysRoleMenu;
import com.lingxu.base.system.manage.mapper.SysMenuMapper;
import com.lingxu.base.system.manage.mapper.SysRoleMapper;
import com.lingxu.base.system.manage.mapper.SysRoleMenuMapper;
import com.lingxu.base.system.manage.mapper.SysUserMapper;
import com.lingxu.base.system.manage.service.ISysRoleMenuService;
import com.lingxu.base.system.manage.util.TreeNodeUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Description: 角色_菜单关联表
 * @Author: LingXu
 * @Date:   2021-01-04
 * @Version: V1.0.1
 */
@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements ISysRoleMenuService {

     @Resource
     private SysRoleMenuMapper sysRoleMenuMapper;
     @Resource
     private SysMenuMapper sysMenuMapper;
     @Resource
     private SysRoleMapper roleMapper;
     @Resource
     private SysUserMapper sysUserMapper;



     @Override
     public void distributeRoleMenus(String roleId, List<String> menuIds) {
          List<SysRoleMenu> sysRoleMenus=new ArrayList<>();
          for(String menuId:menuIds){
               SysRoleMenu sysRoleMenu=new SysRoleMenu();
               sysRoleMenu.setRoleid(roleId);
               sysRoleMenu.setMenuid(menuId);
               sysRoleMenus.add(sysRoleMenu);
          }
          super.saveBatch(sysRoleMenus);
          //判断是否存在《数据接入》菜单
          //修改人员表权限为 01，反之03
          if(menuIds.contains("18b631324e55eab4432f5925dfac28c73")){
               //查询角色id
               List<String> data = roleMapper.findUserIdByRoleId(roleId);
               //修改人员表权限为01
               sysUserMapper.updateQxByUserIds(data,"01");
          }
     }

     @Override
     public Integer cancelRoleMenus(String roleId, String menuIds) {
          String[] split = menuIds.split(",");
          int i = 0;
          for(String s : split){
               i+= sysRoleMenuMapper.cancelRoleMenus(roleId,s);
               if("18b631324e55eab4432f5925dfac28c73".equals(s)){
                    //查询角色id
                    List<String> data = roleMapper.findUserIdByRoleId(roleId);
                    //修改人员表权限为01
                    sysUserMapper.updateQxByUserIds(data,"03");
               }
          }
          return i;
     }

     @Override
     public Integer cancelRoleMenusAll(String roleId) {
          sysRoleMenuMapper.cancelRoleMenusAll(roleId);
          //查询角色id
          List<String> data = roleMapper.findUserIdByRoleId(roleId);
          //修改人员表权限为01
          sysUserMapper.updateQxByUserIds(data,"03");
          return 1;
     }

     @Override
     public void distributeRoleMenusAll(String roleId) {
          //先删除所有关联
          sysRoleMenuMapper.cancelRoleMenusAll(roleId);
          List<SysMenu> menus = sysMenuMapper.queryList(new SysMenu());
          List<SysRoleMenu> sysRoleMenus=new ArrayList<>();
          for(SysMenu menu:menus){
               SysRoleMenu sysRoleMenu=new SysRoleMenu();
               sysRoleMenu.setRoleid(roleId);
               sysRoleMenu.setMenuid(menu.getId());
               sysRoleMenus.add(sysRoleMenu);
          }
          super.saveBatch(sysRoleMenus);
          //查询角色id
          List<String> data = roleMapper.findUserIdByRoleId(roleId);
          //修改人员表权限为01
          sysUserMapper.updateQxByUserIds(data,"01");
     }

     @Override
     public List<MenuTree> queryExistMenuTreeByRoleid(String roleid){
          List<MenuTree> treeNodeList = new ArrayList<>();
          List<MenuTree> list = sysRoleMenuMapper.queryExistMenuTreeByRoleid(roleid);
          TreeNodeUtil treeUtil=new TreeNodeUtil<>();
          treeNodeList.addAll(list);
          List<MenuTree> result=treeUtil.list2TreeNode(treeNodeList);
          return result;
     }
     @Override
     public List<MenuTree> queryNotExistMenuTreeByRoleid(String roleid) {
          List<MenuTree> treeNodeList = new ArrayList<>();
          Set<String> set = new HashSet<>();
          List<String> menuidhbs = sysRoleMenuMapper.queryNotExistMenuIdByRoleid(roleid);
          if(menuidhbs==null || menuidhbs.size()==0){
               return treeNodeList;
          }
          for(String menuidhb:menuidhbs){
               String[] list =menuidhb.split(",");
               set.addAll(Arrays.asList(list));
          }
          String menuids = "";
          for(String menuid:set){
               menuids+=","+menuid;
          }
          menuids=menuids.substring(1);
          List<MenuTree> list = sysRoleMenuMapper.queryNotExistMenuTreeByRoleid(menuids);
          TreeNodeUtil treeUtil=new TreeNodeUtil<>();
          treeNodeList.addAll(list);
          List<MenuTree> result=treeUtil.list2TreeNode(treeNodeList);
          return result;
     }


     @Override
     public int findMenu(List<String> roleids) {
          return sysRoleMenuMapper.findMenu(roleids);
     }
}
